var limitControl = limitControl || {};
var limitControlVue;
var controlGlobal = {};
controlGlobal.clickSortFlag = 0; // 用于点击表头排序ajax锁
controlGlobal.orderType = 'rise';  // rise  涨幅  fiveRise  5分钟涨幅  sell 卖盘  buy 买盘  maxRise 最大涨幅
controlGlobal.sortType = 'desc'; // 降序    asc 升序
controlGlobal.dataType = 'current'; //用于数据统计
controlGlobal.opType = '';  //关闭或者打开push开关
controlGlobal.pageId = 'pdt_pms_269.monitor';
controlGlobal.url_ver = {'url_ver':'SJCGBS-14521'};
limitControl = (function(){
	limitControl.initAjax = function(flag){
		$.ajax({
			type:'get',
			url:hxmConf.hxmAjaxUrl,
			data:{
				'op':'getEstimateLimitUpStock'
			},
			dataType:'json',
			cache:false,
			timeout:10000,
		})
		.fail(function(){
			if(flaf == 'start'){
				$('.load-box').addClass('hide');
				$('.load-error').removeClass('hide');
				tabGlobal.coverErrorFlag = 3;
			}
		})
		.done(function(data){
			if(data.code == 0 && !$.isEmptyObject(data.data)){
				// controlGlobal.urlData = data.data.preLimitControl;
				controlGlobal.urlData = data.data.preLimitStockList;
				var monitorStockList = data.data.monitorStockList;
				for (var i = 0; i < controlGlobal.urlData.length; i++) {
					for (var j = 0; j < monitorStockList.length; j++) {
						if (controlGlobal.urlData[i].stockCode == monitorStockList[j].stockCode) {
							Vue.set(controlGlobal.urlData[i], 'limitRemind', true)
							break
						} else {
							Vue.set(controlGlobal.urlData[i], 'limitRemind', false)
						}
					}
				}
				(flag == 'start') && (limitControl.setLimitStockVue());
				if(flag == 'refresh'){
					getAjaxProductLimit(controlGlobal.urlData);
				}
			}
		})
	},
	limitControl.setLimitStockVue = function(){
		limitControlVue = new Vue({
			el:'.limitEndControl',
			data:{
				stockControlList:[],
				switchFlag:0,
				roseData:1,
				highData:0,
				pushData:0,
				pullData:0,
				riseData:0
			},
			methods:{
				clickTabSortSearch:function(event){  //排序
					if(controlGlobal.clickSortFlag == 0){
						var obj = event.currentTarget;
						var id = $(obj).attr('flag-id');
						controlGlobal.orderType = id;
						controlGlobal.clickSortFlag = 1;
						switch (id) {  //  1  表示正  0  表示逆  2不变
							case "1":
								controlGlobal.orderType = 'rise';
								controlGlobal.dataType = 'current';
								this.roseData = (++this.roseData)%2;
								this.roseData == 1 ? (controlGlobal.sortType = 'desc') : (controlGlobal.sortType = 'asc');
								addInsBackground(obj,this.roseData);
								break;
							case "2":
								controlGlobal.orderType = 'fiveRise';
								controlGlobal.dataType = 'fivemin';
								this.highData = (++this.highData)%2;
								this.highData == 1 ? (controlGlobal.sortType = 'desc') : (controlGlobal.sortType = 'asc');
								addInsBackground(obj,this.highData);
								break;
							case "3":
								controlGlobal.orderType = 'sell';
								controlGlobal.dataType = 'sell';
								this.pushData = (++this.pushData)%2;
								this.pushData == 1 ? (controlGlobal.sortType = 'desc') : (controlGlobal.sortType = 'asc');
								addInsBackground(obj,this.pushData);
								break;
							case "4":
								controlGlobal.orderType = 'buy';
								controlGlobal.dataType = 'buy';
								this.pullData = (++this.pullData)%2;
								this.pullData == 1 ? (controlGlobal.sortType = 'desc') : (controlGlobal.sortType = 'asc');
								addInsBackground(obj,this.pullData);
								break;
							case "5":
								controlGlobal.orderType = 'maxRise';
								controlGlobal.dataType = 'maxrise';
								this.riseData = (++this.riseData)%2;
								this.riseData == 1 ? (controlGlobal.sortType = 'desc') : (controlGlobal.sortType = 'asc');
								addInsBackground(obj,this.riseData);
								break;						
						}
						hxmClickStat(controlGlobal.pageId+'.'+controlGlobal.dataType+'.'+controlGlobal.sortType,controlGlobal.url_ver);
						this.refreshDataControl(0);
					}
				},
				refreshDataControl:function(flag){ //刷新按钮数据
					(flag == 1) && (hxmClickStat(controlGlobal.pageId+'.refresh',controlGlobal.url_ver));
					$.ajax({
						type:'get',
						url:hxmConf.hxmAjaxUrl,
						data:{
							'op':'getEstimateLimitUpStock'
						},
						dataType:'json',
						cache:false,
						timeout:10000,
					})
					.done(function(data){
						if(data.code == 0 && !$.isEmptyObject(data.data)){
							controlGlobal.clickSortFlag = 0;
							controlGlobal.urlData = data.data.preLimitStockList;
							var monitorStockList = data.data.monitorStockList;
							for (var i = 0; i < controlGlobal.urlData.length; i++) {
								for (var j = 0; j < monitorStockList.length; j++) {
									if (controlGlobal.urlData[i].stockCode == monitorStockList[j].stockCode) {
										Vue.set(controlGlobal.urlData[i], 'limitRemind', true)
										break
									} else {
										Vue.set(controlGlobal.urlData[i], 'limitRemind', false)
									}
								}
							}
							//调用接口获取排序数据
							getAjaxProductLimit(controlGlobal.urlData);
						}
					})
					.fail(function(){
						controlGlobal.clickSortFlag = 0;
					})
				},
				handleeventTouch:function(){  //左右开始滑动
					$('.touch_right').hide();
					$('.shadow_left').css({
						'box-shadow' : '0.12rem 0 0.12rem 0 rgba(0,0,0,0.06)'
					});

				},
				handleeventTouchEnd:function(){  //左右滑动结束
					var offsetLeft = $('.product_right_control').offset().left;
					if(controlGlobal.elementSetLeft-30 >= offsetLeft){
						$('.touch_right').hide();
					}else{
						$('.touch_right').show();
					}
					$('.shadow_left').css({
						'box-shadow' : '0 0 0 0 rgba(0,0,0,0)'
					})
				},
				openLimitSwitch:function(flag,event,key,stockCode){  //打开或者关闭push开关
					var obj = event.currentTarget;
					if(flag == 0){
						this.switchFlag = 1;
						controlGlobal.opType = 'setStockMonitor';
						hxmClickStat(controlGlobal.pageId+'.open.'+(key+1),{'url_ver':'SJCGBS-14521','targid':'stock_'+stockCode});
						$(obj).animate({'left':'0.43rem'},'linear',300,function(){
							$(obj).parent().find('span').addClass('addRedBack');
						});
					}else{
						this.switchFlag = 0;
						controlGlobal.opType = 'cancelStockMonitor';
						hxmClickStat(controlGlobal.pageId+'.close.'+(key+1),{'url_ver':'SJCGBS-14521','targid':'stock_'+stockCode});
						$(obj).animate({'left':'0.03rem'},'linear',300,function(){
							$(obj).parent().find('span').removeClass('addRedBack');
						});
					}
					this.$set(this.$data.stockControlList[key],'limitRemind',this.switchFlag);
					//调用push接口，通知后台
					pushSwitchAjax(stockCode);
				}
			}
		});
		Vue.nextTick(function(){
			$('.load-box').addClass('hide');
			limitControlVue.stockControlList = controlGlobal.urlData;
			//调用接口获取排序数据
			getAjaxProductLimit(limitControlVue.stockControlList);
			//滚动到底部埋点
			bottomStat(controlGlobal.pageId+'.bottom',controlGlobal.url_ver);
			setTimeout(function(){
				(limitControlVue.stockControlList.length != 0) && (controlGlobal.elementSetLeft = $('.product_right_control').offset().left);
			},300);
		});

	}

	return {
		initAjax : limitControl.initAjax,
		setLimitStockVue : limitControl.setLimitStockVue
	} 
})();


// 表头点击排序，图标显示
function addInsBackground(obj,num){  
	$(obj).parent().find('ins').removeClass('topup');
	$(obj).parent().find('ins').removeClass('topdown');
	$(obj).parent().find('ins').removeClass('bottomup');
	$(obj).parent().find('ins').removeClass('bottomdown');
	num == 0 ? ($(obj).find('ins:first').addClass('topup')) : ($(obj).find('ins:last').addClass('bottomup'));
}

// 滑动到底部埋点
function bottomStat(stat, extobj){
    $(window).on('scroll', function(){
        var dpr = $('html').data('dpr') ? $('html').data('dpr') : 1; // 页面dpr
        var windowHeight = getPlatform() == 'iphone' ? window.screen.height : $(window).height();//分辨率的高度
        var scrollHeight = $(window).scrollTop() / dpr;//document.documentElement.scrollTop;//滚动的高度
        var totalHeight = $(document).height() / dpr;//文档的总高度
        if((scrollHeight) >= (totalHeight - windowHeight)){
            hxmOnceStat(stat,extobj);
        }
    });
}

// push接口
function pushSwitchAjax(code){

	$.ajax({
		type:'get',
		url:hxmConf.hxmAjaxUrl,
		data:{
			'op':controlGlobal.opType,
			'stockCode': code
		},
		dataType:'json',
		cache:false,
		timeout:10000,
	})
	.done(function(data){
		if(data.code == 0){
		}
	})
}

